<template>
  <el-upload
      class="avatar-uploader"
      :action="pathUrl"
      :show-file-list="false"
      :headers="headers"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload">
    <img style="width: 200px;height: 200px" v-if="test" :src="test" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  </el-upload>

</template>

<script>
export default {
  name: "UploadUtil",
  props:["receive","undorece","test"],

  data(){
    return{
      imageUrl:"",
      pathUrl:'http://localhost:8081/upload',

    }
  },
  computed: {
    headers(){
      const jwt=localStorage.getItem('token')
      return{
        authorization:jwt
      };
    }
  },
  methods:{
    handleAvatarSuccess(res) {
      if (res.code==200){
        console.log(res.data);
        this.receive(res.data)
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },

}
</script>

<style scoped>

</style>
